package com.example.smartcity1.service.patient;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.MsgCodeBean;
import com.example.smartcity1.bean.MyUserInfoBean;
import com.example.smartcity1.bean.PatientNewPostBean;
import com.example.smartcity1.my.MyUserInfoActivity;
import com.example.smartcity1.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class PatientPageActivity extends AppCompatActivity {

    private EditText nameEdit;
    private RadioGroup sexGroup;
    private RadioButton sex1Radio;
    private RadioButton sex0Radio;
    private EditText cardIdEdit;
    private EditText birthdayEdit;
    private EditText phoneEdit;
    private EditText addersEdit;
    private Button button;

    private String token;
    private String sex = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_page);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("创建就诊人");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        token = user.getString("token","");
        initData();
        button.setOnClickListener(v ->{
            if (nameEdit.getText().toString().equals("") || cardIdEdit.getText().toString().equals("") || birthdayEdit.getText().toString().equals("")) {
                Toast.makeText(this, "请正确填写必填项", Toast.LENGTH_SHORT).show();
            } else {
                PatientNewPostBean bean = new PatientNewPostBean();
                bean.setName(nameEdit.getText().toString());
                bean.setCardId(cardIdEdit.getText().toString());
                bean.setBirthday(birthdayEdit.getText().toString());
                if (sex.equals("1")) {
                    bean.setSex(1);
                } else {
                    bean.setSex(0);
                }
                bean.setTel(phoneEdit.getText().toString());
                bean.setAdders(addersEdit.getText().toString());
                bean.setUserId(Integer.valueOf(user.getString("userId", "1")));
                RetrofitClient.serviceService().getPatientNewData(token,bean).enqueue(new Callback<MsgCodeBean>() {
                    @Override
                    public void onResponse(Call<MsgCodeBean> call, Response<MsgCodeBean> response) {
                        MsgCodeBean body = response.body();
                        if (body != null && body.getCode() == 200) {
                            Toast.makeText(PatientPageActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(PatientPageActivity.this, "创建失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<MsgCodeBean> call, Throwable t) {

                    }
                });
            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initView() {
        nameEdit = findViewById(R.id.name_edit);
        sexGroup = findViewById(R.id.sex_group);
        sex1Radio = findViewById(R.id.sex_1_radio);
        sex0Radio = findViewById(R.id.sex_0_radio);
        cardIdEdit = findViewById(R.id.cardId_edit);
        birthdayEdit = findViewById(R.id.birthday_edit);
        phoneEdit = findViewById(R.id.phone_edit);
        addersEdit = findViewById(R.id.adders_edit);
        button = findViewById(R.id.button);
    }
    private void initData() {
        RetrofitClient.appService().getMyUserInfoData(token).enqueue(new Callback<MyUserInfoBean>() {
            @Override
            public void onResponse(Call<MyUserInfoBean> call, Response<MyUserInfoBean> response) {
                MyUserInfoBean myUserInfoBean = response.body();
                if (myUserInfoBean != null && myUserInfoBean.getCode() == 200) {
                    MyUserInfoBean.UserDTO user = myUserInfoBean.getUser();
                    if (user.getSex().equals("1")) {
                        sex = "1";
                        sex1Radio.setChecked(true);
                    } else {
                        sex = "0";
                        sex0Radio.setChecked(false);
                    }
                    nameEdit.setText(user.getNickName());
                    phoneEdit.setText(user.getPhonenumber());
                    if (user.getIdCard() != null) {
                        cardIdEdit.setText(user.getIdCard().toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<MyUserInfoBean> call, Throwable t) {

            }
        });
    }
}